﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace KoPages.CONTROLTEMPLATES.userControls
{
    public partial class BI_SysLog_List : UserControl
    {

        #region Members

        string siteStr = @"http://localhost/BI/";
        string listStr = @"Системный журнал";
        //string listStr = @"SysLog";
        StringBuilder sb = new StringBuilder();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(siteStr))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[listStr];
                    //SPList list = web.GetList(@"/Lists/SysLog");

                    SPQuery query = new SPQuery();
                    // <Where>
                    query.Query = string.Concat(
"<Leq>",
    "<FieldRef Name='DT' />",
    "<Value Type='DateTime'>[Today-5Day(s)]</Value>",
"</Leq>");
                    query.Query += string.Concat(
"<OrderBy>",
    "<FieldRef Name='DT' Ascending='False' />",
"</OrderBy>");

                    // <Where />
                    query.RowLimit = 5;

//                    query.ViewFields = string.Concat(
//"<FieldRef Name='Title' />",
//"<FieldRef Name='DT' />",
//"<FieldRef Name='ows_Mem' />");

                    //query.ViewFieldsOnly = true; // Fetch only the data that we need.

                    SPListItemCollection items = list.GetItems(query);

                    MakeTable(site, web, items);
                }
            }
        }

        protected Exception MakeTable(SPSite site, SPWeb web, SPListItemCollection listItems)
        {
            Exception ret = null;
            MakeTableTitle();
            string clrBk = "#FFFFFF", clr1 = "#FFFFFF", clr2 = "#FFFFE1";
            int count = -1;
            foreach (SPItem item in listItems)
            {
                count++;
                string name = item["Title"].ToString();
                string clrWarn = "#E6FFE6";
                DateTime? dt = null;
                if(item["Дата"]!=null)
                    dt = (DateTime)item["Дата"];
                string note = "";
                if (item["ows_Mem"] != null)
                    note = item["ows_Mem"].ToString();
                if (item["ErrCount"] != null)
                {
                    if (Convert.ToInt32(item["ErrCount"]) > 0)
                        clrWarn = "#FFCCCC";
                }

                if (count % 2 > 0)
                    clrBk = clr1;
                else
                    clrBk = clr2;

                // Response.Write(name + " ; " + dateStr + " ; " + note + @"<br \>");
                MakeTableRow(site, web, name, dt, note, item.ID, listItems.List.ID.ToString(), clrBk, clrWarn);
            }
            oBody.Text += sb.ToString();
            MakeTableBottom();
            return ret;
        }

        protected Exception MakeTableTitle()
        {
            Exception ret = null;
            oBody.Text =
@"
<table style='width:100%' >
    <tr>
        <td style='background-color: #003300; font-size: small; font-weight: bold; color: #FFFFCC'>
            &nbsp;&nbsp;Дата</td>
        <td style='background-color: #003300; font-size: small; font-weight: bold; color: #FFFFCC'>
            </td>
        <td style='background-color: #003300; font-size: small; font-weight: bold; color: #FFFFCC'>
            Описание</td>
    </tr>    
";
            return ret;
        }

        protected Exception MakeTableBottom()
        {
            Exception ret = null;
            oBody.Text += @"</table>";
            return ret;
        }

        protected Exception MakeTableRow(SPSite site, SPWeb web, string title, DateTime? dt, string mem, int id, string listIDStr, string clrBk, string clrWarn)
        {
// http://sp.korablik.local/BI/_layouts/listform.aspx?PageType=4&ListId={10B878DF-4BEF-4191-B853-9E39087D6AB3}&ID=75&ContentTypeID=0x01004270AE0B85856D4EA62E323A3A44FC6A
            Exception ret = null;
            string dateStr = ((dt == null) ? "" : ((DateTime)dt).ToShortDateString());

            string siteItemUrl =
@"http://sp.korablik.local/BI/_layouts/listform.aspx?PageType=4&ListId={"
+ listIDStr
+@"}&ID=" + id.ToString()
+@"&ContentTypeID=0x01004270AE0B85856D4EA62E323A3A44FC6A";

            string str =
@"<a href='" + siteItemUrl + "' target='_blank' title='" 
+ "" 
+ "' >" + title + "</a>";

            sb.Append(
                @"<tr><td >" + dateStr
+ "&nbsp;&nbsp;</td><td style='background-color: " + clrWarn + "' >" + str
+ "</td><td style='background-color: " + clrBk +"' > <p style='font-size: small' >" + SPEncode.HtmlDecode(mem)
+ "</p></td></tr>");
            return ret;
        }



    }
}
